package org.jeecg.modules.engineering.service.impl;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.constant.CacheConstant;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.StringUtils;
import org.jeecg.common.util.UUIDGenerator;
import org.jeecg.modules.engineering.entity.EquipCollectItem;
import org.jeecg.modules.engineering.entity.EquipCollectItemDetail;
import org.jeecg.modules.engineering.entity.EquipCollectTrigger;
import org.jeecg.modules.engineering.entity.EquipEventSort;
import org.jeecg.modules.engineering.mapper.EquipCollectTriggerMapper;
import org.jeecg.modules.engineering.mapper.EquipCollectItemDetailMapper;
import org.jeecg.modules.engineering.mapper.EquipEventSortMapper;
import org.jeecg.modules.engineering.service.IEquipCollectItemDetailService;
import org.jeecg.modules.engineering.service.IEquipCollectTriggerService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 每个小区的采集项
 * @Author: jeecg-boot
 * @Date:   2019-09-12
 * @Version: V1.0
 */
@Service
public class EquipCollectItemDetailServiceImpl extends ServiceImpl<EquipCollectItemDetailMapper, EquipCollectItemDetail> implements IEquipCollectItemDetailService {

	@Autowired
	private EquipCollectItemDetailMapper equipCollectItemDetailMapper;
	@Autowired
	private EquipCollectTriggerMapper equipCollectTriggerMapper;
	@Autowired
	private IEquipCollectTriggerService equipCollectTriggerService;

	@Autowired
	private RedisUtil redisUtil;
	
	@Override
	@Transactional
	public void saveMain(EquipCollectItemDetail equipCollectItemDetail, List<EquipCollectTrigger> equipCollectTriggerList) {
		int count = equipCollectItemDetailMapper.insert(equipCollectItemDetail);
		for(EquipCollectTrigger entity:equipCollectTriggerList) {
			//外键设置
			entity.setItemId(equipCollectItemDetail.getId());
			entity.setId(UUIDGenerator.getUUID());
			equipCollectTriggerMapper.insert(entity);
		}
		if(count > 0){
			//以ID为key，将小区采集项存储到redis
			setToRedis(equipCollectItemDetail);

		}
	}

	@Override
	@Transactional
	public void updateMain(EquipCollectItemDetail equipCollectItemDetail,List<EquipCollectTrigger> equipCollectTriggerList) {
		EquipCollectItemDetail oldEntity = getById(equipCollectItemDetail.getId());
		int count = equipCollectItemDetailMapper.updateById(equipCollectItemDetail);
		
		//1.先删除子表数据
		equipCollectTriggerMapper.deleteByMainId(equipCollectItemDetail.getId());
		
		//2.子表数据重新插入
		for(EquipCollectTrigger entity:equipCollectTriggerList) {
			//外键设置
			entity.setItemId(equipCollectItemDetail.getId());
			entity.setId(UUIDGenerator.getUUID());
			equipCollectTriggerMapper.insert(entity);
		}
		if(count > 0){
			//先移除编辑前的redis键值
			removeFromRedis(oldEntity);
			//更新成功后，重新设置redis
			setToRedis(equipCollectItemDetail);
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		EquipCollectItemDetail entity = getById(id);
		boolean result = equipCollectTriggerMapper.deleteByMainId(id);
		equipCollectItemDetailMapper.deleteById(id);
		if(result){
			//从redis删除
			removeFromRedis(entity);
		}
	}

	@Override
	@Transactional
	public void delBatchMain(List<String> idList) {
		for(String id:idList) {
			delMain(id);
//			equipCollectTriggerMapper.deleteByMainId(id.toString());
//			equipCollectItemDetailMapper.deleteById(id);
		}
	}

	private void setToRedis(EquipCollectItemDetail entity){
		if(entity == null){
			return;
		}
		redisUtil.set(CacheConstant.REDIS_TABLES+"equip_collect_item_detail:id:"+entity.getId(), JSONObject.toJSONString(entity));
		redisUtil.set(CacheConstant.REDIS_TABLES+"equip_collect_item_detail:otherKey:"+entity.getProjectId()+"_"+entity.getItemId(), entity.getId());
		//存储采集项相关的事件触发规则信息到redis
		equipCollectTriggerService.setToRedis(entity.getId());
	}

	@Override
	public void removeFromRedis(String id){
		removeFromRedis(findById(id));
	}

	@Override
	public void removeFromRedis(EquipCollectItemDetail entity){
		if(entity == null){
			return;
		}
		redisUtil.del(CacheConstant.REDIS_TABLES+"equip_collect_item_detail:id:"+entity.getId()
				,CacheConstant.REDIS_TABLES+"equip_collect_item_detail:otherKey:"+entity.getProjectId()+"_"+entity.getItemId()
		);

		//从redis中删除采集项相关的事件触发规则信息
		equipCollectTriggerService.removeFromRedis(entity.getId());
	}


	@Override
	public EquipCollectItemDetail getFromRedisByOtherKey(String key){
		String id = (String) redisUtil.get(CacheConstant.REDIS_TABLES+"equip_collect_item_detail:otherKey:"+key);
		if(StringUtils.isEmpty(id)){
			return null;
		}
		return getFromRedisById(id);
	}

	@Override
	public EquipCollectItemDetail getFromRedisById(String id){
		EquipCollectItemDetail entity = JSONObject.parseObject((String) redisUtil.get(CacheConstant.REDIS_TABLES+"equip_collect_item_detail:id:"+id),EquipCollectItemDetail.class) ;
		if(entity == null){
			//从数据库查询
			entity = findById(id);
			if(entity != null){
				//重新设置redis
				setToRedis(entity);
			}
		}
		return entity;
	}

	private EquipCollectItemDetail findById(String id) {
		return equipCollectItemDetailMapper.findById(id);
	}


	@Override
	public Result<String> refreshToRedis() {
		List<EquipCollectItemDetail> lists = list();
		if(lists != null && lists.size() > 0){
			for (EquipCollectItemDetail entity : lists){
				setToRedis(entity);
			}
		}
		return Result.ok();
	}

	@Override
	public List<EquipCollectItemDetail> queryListByProjectId(String projectId) {

		return equipCollectItemDetailMapper.queryListByProjectId(projectId);
	}

	@Override
	public boolean checkHave(String projectId, String itemId, String id) {
		long count = equipCollectItemDetailMapper.getCount(projectId,itemId,id);
		return count > 0;
	}

}
